package com.zua.controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@CrossOrigin(origins = "*",allowCredentials = "true")
@RestController
@RequestMapping("/file")
public class FileController {
    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public Map upload(MultipartFile file) {
        Date date = new Date();
        int str = date.hashCode();
        Map map=new HashMap();
        String fileName = file.getOriginalFilename();
        String newFileName = str+fileName;
        String path="E:\\IDEAcode\\ZuaMarket\\src\\main\\resources\\static\\good\\upload";
        assert fileName != null;
        File dir = new File(path, newFileName);
       try {
            file.transferTo(dir);
            map.put("state","success");
            map.put("fileName",newFileName);
        } catch (IOException e) {
            e.printStackTrace();
            map.put("state","false");
        }
        return  map;
    }
}
